package app

import (
	"fmt"
	"game-server/app/common/request"
	"game-server/app/common/response"
	"game-server/app/services"

	"github.com/gin-gonic/gin"
)

// ArticleList 文章列表
// @Summary 文章列表
// @Description 文章列表
// @Tags 文章管理
// @Accept json
// @Produce json
// @Param data body request.ArticleListRequest true "请求参数"
// @Router /gameServer/admin/article/list [post]
func ArticleList(c *gin.Context) {
	var form request.ArticleListRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	data, err := services.ArticleServiceApp.GetList(form)
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}
	response.Success(c, data)
}

// ArticleAdd 文章添加
// @Summary 文章添加
// @Description 文章添加
// @Tags 文章管理
// @Accept json
// @Produce json
// @Param data body request.ArticleAddRequest true "请求参数"
// @Router /gameServer/admin/article/add [post]
func ArticleAdd(c *gin.Context) {
	var form request.ArticleAddRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	if err := services.ArticleServiceApp.Add(form); err != nil {
		response.BusinessFail(c, err.Error())
		return
	}
	response.Success(c, nil)
}

// 更新文章
// @Summary 更新文章
// @Description 更新文章
// @Tags 文章管理
// @Accept json
// @Produce json
// @Param data body request.ArticleUpdateRequest true "请求参数"
// @Router /gameServer/admin/article/update [post]
func ArticleUpdate(c *gin.Context) {
	var form request.ArticleUpdateRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	if err := services.ArticleServiceApp.Update(form); err != nil {
		response.BusinessFail(c, err.Error())
		return
	}
	response.Success(c, nil)
}

// ArticleDelete
// @Summary 文章删除
// @Description 文章删除
// @Tags 文章管理
// @Accept json
// @Produce json
// @Param data body request.ArticleDeleteRequest true "请求参数"
// @Router /gameServer/admin/article/delete [post]
func ArticleDelete(c *gin.Context) {
	var form request.ArticleDeleteRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	if err := services.ArticleServiceApp.Delete(form); err != nil {
		response.BusinessFail(c, err.Error())
		return
	}
	response.Success(c, nil)
}

// ArticleDetail
// @Summary 文章详情
// @Description 文章详情
// @Tags 文章管理
// @Accept json
// @Produce json
// @Param data body request.ArticleDetailRequest true "请求参数"
// @Router /gameServer/admin/article/detail [post]
func ArticleDetail(c *gin.Context) {
	var form request.ArticleDetailRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	data, err := services.ArticleServiceApp.Detail(form)
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}
	response.Success(c, data)
}

// ArticleRandom
// @Summary 随机获取文章
// @Description 随机获取文章
// @Tags 文章管理
// @Accept json
// @Produce json
// @Param data body request.ArticleRandomRequest true "请求参数"
// @Router /gameServer/admin/article/random [post]
func ArticleRandom(c *gin.Context) {
	var form request.ArticleRandomRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	fmt.Println(form, "form")
	data, err, count := services.ArticleServiceApp.Random(form)
	if err != nil {
		response.BusinessFail(c, err.Error())
		return
	}
	response.Success(c, map[string]interface{}{
		"data":  data,
		"count": count,
	})

}

// ArticleCommentAdd
// @Summary 文章评论添加
// @Description 文章评论添加
// @Tags 文章管理
// @Accept json
// @Produce json
// @Param data body request.ArticleCommentAddRequest true "请求参数"
// @Router /gameServer/admin/article/comment/add [post]
func ArticleCommentAdd(c *gin.Context) {
	var form request.ArticleCommentAddRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	if err := services.ArticleServiceApp.CommentAdd(form); err != nil {
		response.BusinessFail(c, err.Error())
		return
	}
	response.Success(c, nil)
}

// ArticleCommentUpdate
// @Summary 文章评论更新
// @Description 文章评论更新
// @Tags 文章管理
// @Accept json
// @Produce json
// @Param data body request.ArticleCommentUpdateRequest true "请求参数"
// @Router /gameServer/admin/article/comment/update [post]
func ArticleCommentUpdate(c *gin.Context) {
	var form request.ArticleCommentUpdateRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	if err := services.ArticleServiceApp.CommentUpdate(form); err != nil {
		response.BusinessFail(c, err.Error())
		return
	}
	response.Success(c, nil)
}

// / ArticleCommentDelete
// @Summary 文章评论删除
// @Description 文章评论删除
// @Tags 文章管理
// @Accept json
// @Produce json
// @Param data body request.ArticleCommentDeleteRequest true "请求参数"
// @Router /gameServer/admin/article/comment/delete [post]
func ArticleCommentDelete(c *gin.Context) {
	var form request.ArticleCommentDeleteRequest
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	if err := services.ArticleServiceApp.CommentDelete(form); err != nil {
		response.BusinessFail(c, err.Error())
		return
	}
	response.Success(c, nil)
}
